﻿using MyTest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Collection : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        #region 关联性泛型集合（Key-Value）

        #region Dictionary（哈希表）
        Dictionary<string, Product2> dictProducts = new Dictionary<string, Product2>();
        dictProducts.Add("t1", new Product2());
        dictProducts.Add("t2", new Product2());
        dictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小：" + dictProducts.Count + "<br>");
        Product2 p;
        Response.Write( dictProducts.TryGetValue("t1",out p)+"<br>");
        StringBuilder sb = new StringBuilder();
        //key 键的遍历，Values  字典值的集合
        foreach (var key in dictProducts.Keys)
        {
            sb.Append(key + "\t");
        }
        Response.Write(sb.ToString()+"<br>");
        #endregion

        #region SortedDictionary（二叉树）
        SortedDictionary<string, Product2> sDictProducts = new SortedDictionary<string, Product2>();
        sDictProducts.Add("t1", new Product2());
        sDictProducts.Add("t4", new Product2());
        sDictProducts.Add("t2", new Product2());
        sDictProducts.Add("t3", new Product2());
        Response.Write("字典集合大小：" + dictProducts.Count + "<br>");
        Response.Write(sDictProducts.ContainsKey("t10") + "<br>");
        StringBuilder sb2 = new StringBuilder();
        foreach (var key in sDictProducts.Keys)
        {
            sb2.Append(key + "\t");
        }
        Response.Write(sb2.ToString() + "<br>");
        #endregion

        #region SortedList(数组)
        SortedList<string, Product2> sListProducts = new SortedList<string, Product2>();
        sListProducts.Add("t3", new Product2());
        sListProducts.Add("t1", new Product2());
        sListProducts.Add("t2", new Product2());
        StringBuilder sb3 = new StringBuilder();
        foreach (var key in sListProducts.Keys)
        {
            sb3.Append(key + "\t");
        }
        Response.Write(sb3.ToString() + "<br>");
        #endregion

        #endregion

        #region 非关联性泛型集合
        #region List(数组)
        List<Product2> lProducts = new List<Product2>();
        lProducts.Add(new Product2());
        Response.Write("集合元素个数 ："+lProducts.Count + "<br>");
        Response.Write("第一个元素的哈希值："+ lProducts[0].GetHashCode() + "<br>");
        #endregion

        #region LinkedList(双向链表)
        LinkedList<Product2> llProducts = new LinkedList<Product2>();
        llProducts.AddLast(new Product2("aa","type" + "<br>") );
        llProducts.AddFirst(new Product2("bb", "type" + "<br>") );
        StringBuilder sb4 = new StringBuilder();
        foreach (var product in llProducts)
        {
            sb4.Append(product.Name + "\t");
        }
        Response.Write(sb4.ToString() + "<br>");
        #endregion

        #region HashSet(哈希表)“相同的对象：判断两个对象相等”
        HashSet<Product2> hsProducts = new HashSet<Product2>();
        hsProducts.Add(new Product2("cc", "type" + "<br>"));
        hsProducts.Add(new Product2("aa", "type" + "<br>"));
        hsProducts.Add(new Product2("aa", "type" + "<br>"));
        hsProducts.Add(new Product2("bb", "type" + "<br>"));
        StringBuilder sb5 = new StringBuilder();
        foreach (var product in hsProducts)
        {
            sb5.Append(product.Name + "\t");
        }
        Response.Write(sb5.ToString() + "<br>");
        #endregion

        #region SortedSet（二叉树）：排序加唯一
        SortedSet<Product2> ssProducts = new SortedSet<Product2>();
        ssProducts.Add(new Product2("cc", "type"));
        ssProducts.Add(new Product2("aa", "type"));
        ssProducts.Add(new Product2("aa", "type"));
        ssProducts.Add(new Product2("bb", "type"));
        StringBuilder sb6 = new StringBuilder();
        foreach (var product in ssProducts)
        {
            sb6.Append(product.Name + "\t");
        }
        Response.Write(sb6.ToString() + "<br>");
        #endregion

        #endregion
    }
}